#include "Stdafx.h"
#include "SceneViewManager.h"
#include "ConsoleForm.h"

using namespace OgreUnified;

SceneViewManager::SceneViewManager():mSceneViewNumber(0),mSceneMgr(0)
{
	Instance = this;
	mRendering = false;
}

SceneViewManager::~SceneViewManager()
{

}


SceneViewForm^ SceneViewManager::addSceneView()
{
	if(mSceneMgr==NULL)
		mSceneMgr = Root::getSingletonPtr()->createSceneManager(SceneType::ST_GENERIC);

	SceneViewForm^ renderView = gcnew SceneViewForm(mSceneViewNumber++,mSceneMgr);
	renderView->Closed += gcnew EventHandler(this, &SceneViewManager::SceneViewClosed);
	mSceneViews.Add(renderView);
	return renderView;
}

void SceneViewManager::SceneViewClosed(Object^sender, EventArgs^e)
{
	SceneViewForm^ renderform = (SceneViewForm^)sender;
	renderform->Closed -= gcnew EventHandler(this, &SceneViewManager::SceneViewClosed);
	mSceneViews.Remove(renderform);
}

void SceneViewManager::removeSceneView(SceneViewForm^ form)
{
	mSceneViews.Remove(form);	
}

void SceneViewManager::removeAllSceneView()
{
	for each(SceneViewForm^ sview in mSceneViews)
	{
		delete sview;
	}
	mSceneViews.Clear();
}

void SceneViewManager::StartRendering()
{
	mRendering = true;

	while(mRendering)
	{
		Root::getSingletonPtr()->renderOneFrame();
		System::Windows::Forms::Application::DoEvents();
	}	
}

void SceneViewManager::StopRendering()
{
	mRendering = false;
}